package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.base.BpBaseFragment
import com.bp.hearthealth.pulse.lnapps.databinding.LifeStyleArticelLayoutBgBinding
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_main.BpMainActivity
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_adapter.BpLifeStyleArticelAdapter
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_bean.BpLifeStyleBean
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.bp_view.BPSpacingItemDecoration
import java.io.Serializable

class BpLifeStyleArticelFragment : BpBaseFragment<LifeStyleArticelLayoutBgBinding>() {

    private val lifeStyleArticelAdapter = BpLifeStyleArticelAdapter()

    companion object {
        private const val _LIST = "lief_style_list"
        fun newInstance(modelList: MutableList<BpLifeStyleBean>): Fragment {
            val args = Bundle()
            args.putSerializable(_LIST, modelList as Serializable)
            val fragment = BpLifeStyleArticelFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initRecyclerView()
        try {
            val list = (arguments?.getSerializable(_LIST) as MutableList<*>)
            lifeStyleArticelAdapter.setListData(list as MutableList<BpLifeStyleBean>)
        } catch (e: Exception) {
            LogUtil.e(e)
        }
    }

    private fun initRecyclerView() {
        binding.apply {
            val layoutManager = LinearLayoutManager(activity)
            layoutManager.orientation = LinearLayoutManager.VERTICAL
            recyclerView.layoutManager = layoutManager
            recyclerView.adapter = lifeStyleArticelAdapter
            lifeStyleArticelAdapter.setActivity(activity)
            val marginTop = resources.getDimensionPixelSize(R.dimen.life_style_list_margin_top)
            val marginBottom =
                resources.getDimensionPixelSize(R.dimen.life_style_list_margin_bottom)
            recyclerView.addItemDecoration(BPSpacingItemDecoration(marginBottom, marginTop))
            lifeStyleArticelAdapter.setOnItemClickLisener(object :
                BpLifeStyleArticelAdapter.OnItemClickListener {
                override fun onItemClick(tip: String, drawableId: Int) {
                    (activity as BpMainActivity).startArticleActivity(
                        tip,
                        drawableId,
                        Constants.LIFE_STYLE_ARTICE_FRAGMENT_VIEW
                    )
                }
            })
        }
    }
}